﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace 账号管理
{
    public class Function
    {
        public static bool ValiKey(string pathName)
        {
            IDatabase db = DatabaseFactory.CreateDatabase("sqlite");
            KeyData data = db.ExecuteDataReader<KeyData>("select 键,签名 from 钥匙", null, dr =>
            {
                return new KeyData
                {
                    Key = dr["键"].GetValue<string>(string.Empty),
                    PlcKey = dr["签名"].GetValue<string>(string.Empty),
                };
            }).FirstOrDefault();

            if (data == null) return false;

            using (FileStream fs = new FileStream(pathName, FileMode.Open))
            {
                string bSign = System.Text.Encoding.UTF8.GetString(fs.ReadAllBinary());

                return XEncrypt.VerifySigned(data.Key, bSign, data.PlcKey);
            }
        }

        /// <summary>
        /// 缓存用户
        /// </summary>
        internal static UserData CacheUser;
    }
}
